angular.module('classapp', [])
.controller(
		'classCtrl',
function($scope, $http, $timeout) {
	// 判断树的显示隐藏
	$scope.treeView = true;
	// 默认第几页
	$scope.pageOpt = [ {
		num : "1"
	} ];
	$scope.localOpt = $scope.pageOpt[0].num;
	$scope.page = "1";
	$scope.index = "";// 用于检验是否点击两次同一个checkbox
	// 查询班级信息
	$scope.searchClassInfo = function(schoolName, className,
			gradeName, nodeId, page, rows) {
		$http
				.post(
						path
								+ "/school/classinfo/schoolClassSearch",
						{
							params : {
								"classBean.schoolName" : schoolName,
								"classBean.className" : className,
								"classBean.gradeName" : gradeName,
								nodeId : nodeId,
								rows : rows,
								page : page
							}
						})
				.success(
						function(res) {
							$scope.info = res.rows;
							/* $scope.recordCount=res.rows.length; */// 此分页的条数
							$scope.total = res.total;// 总条数
							if ((parseInt($scope.total)
									% parseInt($scope.row) == 0)) {
								$scope.totalCount = parseInt((parseInt($scope.total) / parseInt($scope.row)));
							} else {
								$scope.totalCount = parseInt((parseInt($scope.total) / parseInt($scope.row))) + 1;// 总页数
							}
							$scope.pageOpt = [];
							for (var i = 1; i < $scope.totalCount + 1; i++) {
								$scope.pageOpt.push({
									num : i
								});
							}
							// 首页、上一页、下一页、尾页的样式
							if ($scope.localOpt < $scope.totalCount) {
								if ($("#homepage").hasClass(
										"sms_pageredbtn")) {
									$("#homepage").removeClass(
											"sms_pageredbtn");
								}
								if ($("#pre").hasClass(
										"sms_pageredbtn")) {
									$("#pre").removeClass(
											"sms_pageredbtn");
								}
								$("#next").addClass(
										"sms_pageredbtn");
								$("#last").addClass(
										"sms_pageredbtn");
							}
							if ($scope.localOpt > 1) {
								if ($("#next").hasClass(
										"sms_pageredbtn")) {
									$("#next").removeClass(
											"sms_pageredbtn");
								}
								if ($("#last").hasClass(
										"sms_pageredbtn")) {
									$("#last").removeClass(
											"sms_pageredbtn");
								}
								$("#homepage").addClass(
										"sms_pageredbtn");
								$("#pre").addClass(
										"sms_pageredbtn");
							}
							if ($scope.localOpt < $scope.totalCount
									&& $scope.localOpt > 1) {
								$("#homepage").addClass(
										"sms_pageredbtn");
								$("#pre").addClass(
										"sms_pageredbtn");
								$("#next").addClass(
										"sms_pageredbtn");
								$("#last").addClass(
										"sms_pageredbtn");
							}
						})

	}

	// 加载树
	$scope.loadTree = function(event) {
		$scope.schoolName = "";
		$scope.schoolId = "";
		$scope.index = "";
		/* addNewNode 新增 判断是否选左边的树 */
		$scope.addNewNode = false;
		$scope.searchClassInfo("", "", "", "", $scope.page,
				$scope.row);
		$scope.setting = {
			async : {
				enable : true,
				type : "post",
				url : path + "/school/schoolinfo/getSchoolTree",
				autoParam : [ "admareaCode" ]
			},
			data : {
				key : {
					name : "admareaName"
				},
				simpleData : {
					enable : true,
					idKey : "admareaCode",
					pIdKey : "supAreaCode"
				}
			},
			callback : {
				onClick : function zTreeOnClick(event, treeId,
						treeNode) {
					$scope.schoolName = treeNode.schoolName;
					$scope.schoolId = treeNode.schoolId;
					$scope.index = "";
					/* addNewNode 新增 判断是否选左边的树 */
					$scope.addNewNode = true;
					$scope.searchClassInfo($scope.schoolName,
							"", "", treeNode.admareaCode,
							$scope.page, $scope.row);

				}
			}
		};
		$.fn.zTree.init($("#menuTree"), $scope.setting);

	}
	// 调用加载树
	$scope.loadTree();
	// 刷新
	$(".icon-reload").click(function() {
		$scope.loadTree();
	});

	/* $scope.searchInfo("","",$scope.row,1); */
	// 判断是否是提示信息 true:提示信息 false：其他页面
	$scope.isMessage = false;
	// 判断是否是新增、修改、查看页面
	$scope.classInfoOption = true;
	// 判断是否是查看页面
	$scope.isSearchPage = false;
	// 隐藏树
	$scope.changeTreeState = function() {
		$scope.treeView = !$scope.treeView;
		if ($scope.treeView) {
			$(".rightDiv").css("width", "78%");
		} else {
			$(".rightDiv").css("width", "97%");
		}
	}
	// 查询
	$scope.search = function() {
		$scope.localOpt = "1";
		$scope.searchClassInfo($scope.seschoolName,
				$scope.seclassName, $scope.segradeName, "",
				$scope.localOpt, $scope.row);

	}
	// 重置（将查询条件清空）
	$scope.resetSearch = function() {
		$scope.seschoolName = "";
		$scope.seclassName = "";
		$scope.segradeName = "";
		if ($scope.addNewNode == true) {
			$scope.searchClassInfo($scope.schoolName, "", "",
					"", $scope.localOpt, $scope.row);
		} else {
			$scope.searchClassInfo("", "", "", "",
					$scope.localOpt, $scope.row);
		}

	}

	// 选择每页条数
	$scope.row = "20";
	$scope.searchClassInfo("", "", "", "", 1, "20");

	$scope.listRow = function() {
		$scope.localOpt = "1";
		if ($scope.addNewNode == true) {
			$scope.searchClassInfo($scope.schoolName, "", "",
					"", $scope.localOpt, $scope.row);
		} else {
			$scope.searchClassInfo("", "", "", "",
					$scope.localOpt, $scope.row);
		}
	}
	// 选择页数
	$scope.pageChoose = function() {

		if ($scope.addNewNode == true) {
			$scope.searchClassInfo($scope.schoolName, "", "",
					"", $scope.localOpt, $scope.row);
		} else {
			$scope.searchClassInfo("", "", "", "",
					$scope.localOpt, $scope.row);
		}
	}

	// 首页
	$scope.toHomepage = function() {
		if ($("#homepage").hasClass("sms_pageredbtn")) {
			$scope.localOpt = "1";
			if ($scope.addNewNode == true) {
				$scope.searchClassInfo($scope.schoolName, "",
						"", "", $scope.localOpt, $scope.row);
			} else {
				$scope.searchClassInfo("", "", "", "",
						$scope.localOpt, $scope.row);
			}
		}
	}
	// 尾页
	$scope.toLastpage = function() {
		if ($("#last").hasClass("sms_pageredbtn")) {
			$scope.localOpt = $scope.pageOpt[$scope.pageOpt.length - 1].num
					.toString();
			if ($scope.addNewNode == true) {
				$scope.searchClassInfo($scope.schoolName, "",
						"", "", $scope.localOpt, $scope.row);
			} else {
				$scope.searchClassInfo("", "", "", "",
						$scope.localOpt, $scope.row);
			}
		}
	}
	// 上一页
	$scope.prePage = function() {
		if ($scope.localOpt == "1") {
			return;
		}
		$scope.localOpt = ($scope.localOpt - 1).toString();
		if ($scope.addNewNode == true) {
			$scope.searchClassInfo($scope.schoolName, "", "",
					"", $scope.localOpt, $scope.row);
		} else {
			$scope.searchClassInfo("", "", "", "",
					$scope.localOpt, $scope.row);
		}
	}

	// 下一页
	$scope.nextPage = function() {
		if ($scope.localOpt == $scope.pageOpt[$scope.pageOpt.length - 1].num
				.toString()) {
			return;
		}
		$scope.localOpt = ($scope.localOpt - 0 + 1).toString();
		if ($scope.addNewNode == true) {
			$scope.searchClassInfo($scope.schoolName, "", "",
					"", $scope.localOpt, $scope.row);
		} else {
			$scope.searchClassInfo("", "", "", "",
					$scope.localOpt, $scope.row);
		}

	}
	// 学校对应的班级下拉列表
	$scope.getGrades = function(schoolId) {
		$http.get(path + "/school/classinfo/getGrades", {
			params : {
				schoolId : schoolId
			}
		}).success(function(res) {
			$scope.getGradeInfo = res;
		});

	}
	// 保存信息
	$scope.saveInfo = function() {
		if ($scope.gradeName == "") {
			layer.tips('该项为必填项', $(".add_warning"), {
				tips : [ 1, '#111' ],
				time : 1000
			});
			return;
		}
		if ($scope.className == "") {
			layer.tips('该项为必填项', $(".add_warning_class"), {
				tips : [ 1, '#111' ],
				time : 1000
			});
			return;
		}
		$http
				.get(
						path + "/school/classinfo/save",
						{
							params : {
								"classBean.className" : $scope.className,
								"classBean.schoolName" : $scope.schoolName,
								"classBean.studentsNum" : $scope.studentsNum,
								"classBean.schoolId" : $scope.schoolId,
								"classBean.gradeId" : $scope.gradeId,
								"classBean.gradeName" : $scope.gradeName,
								"classBean.classId" : $scope.classId
							}
						})
				.success(
						function(res) {
							$(".addPage").addClass("hide");
							layer.close($scope.addLayer);
							$timeout(
									function() {
										if ($scope.actionEvent == 0) {
											layer
													.open({
														type : 1,
														title:'新增',
														skin : 'layui-layer-demo', // 样式类名
														closeBtn : 1, // 显示关闭按钮
														shift : 2,
														area : [
																'300px',
																'' ], // 宽高
														shadeClose : false, // 开启遮罩关闭
														btn : [ '确定' ],
														content : '&nbsp;&nbsp;&nbsp;保存成功！'
													});
										} else {
											layer
													.open({
														type : 1,
														title:'修改',
														skin : 'layui-layer-demo', // 样式类名
														closeBtn : 1, // 显示关闭按钮
														shift : 2,
														area : [
																'300px',
																'' ], // 宽高
														shadeClose : false, // 开启遮罩关闭
														btn : [ '确定' ],
														content : '&nbsp;&nbsp;&nbsp;修改成功！'
													});
										}
										if ($scope.addNewNode == true) {
											$scope
													.searchClassInfo(
															$scope.schoolName,
															"",
															"",
															"",
															$scope.localOpt,
															$scope.row);
										} else {
											$scope
													.searchClassInfo(
															"",
															"",
															"",
															"",
															$scope.localOpt,
															$scope.row);
										}
										$scope.className = "";
										$scope.classId = "";
										$scope.gradeName = "";
										$scope.gradeId = "";
										$scope.studentsNum = "";
										$scope.index = "";
									}, 1000)
						});
	}
	// 删除信息
	$scope.deleteInfo = function() {
		$http.get(path + "/school/classinfo/delete", {
			params : {
				"classBean.classId" : $scope.classId
			}
		}).success(
				function(res) {
					$scope.schoolName = "";
					layer.open({
						type : 1,
						title:'删除',
						skin : 'layui-layer-demo', // 样式类名
						closeBtn : 1, // 显示关闭按钮
						shift : 2,
						area : [ '300px', '' ], // 宽高
						shadeClose : false, // 开启遮罩关闭
						btn : [ '确定' ],
						content : '&nbsp;&nbsp;&nbsp;删除成功！'
					});
					$scope.index = "";
					if ($scope.addNewNode == true) {
						$scope.searchClassInfo(
								$scope.schoolName, "", "", "",
								$scope.localOpt, $scope.row);
					} else {
						$scope.searchClassInfo("", "", "", "",
								$scope.localOpt, $scope.row);
					}
				});
	}
	// 班级发生变化时
	$scope.gradeChange = function() {
		$scope.gradeId = $(".gradeSelect").find(
				"option:checked").attr("id");
	}
	// 增
	$scope.add = function() {
		$scope.actionEvent = 0;
		if ($scope.addNewNode == true) {
			$scope.className = "";
			$scope.classId = "";
			$scope.gradeName = "";
			$scope.gradeId = "";
			$scope.studentsNum = "";
			$(".addPage").removeClass("hide");
			$scope.getGrades($scope.schoolId);
			$scope.addLayer = layer.open({
				type : 1,
				title:'新增',
				skin : 'layui-layer-demo', // 样式类名
				closeBtn : 1, // 显示关闭按钮
				shift : 2,
				area : [ '400px', '' ], // 宽高
				shadeClose : false, // 开启遮罩关闭
				btn : [ '确定', '取消' ],
				yes : function(index, layero) { // 或者使用btn1
					$scope.saveInfo();

				},
				cancel : function(index) { // 或者使用btn2
				},
				content : $(".addPage")
			});
		} else {
			layer.open({
				type : 1,
				title:'新增',
				skin : 'layui-layer-demo', // 样式类名
				closeBtn : 1, // 显示关闭按钮
				shift : 2,
				area : [ '300px', '' ], // 宽高
				shadeClose : false, // 开启遮罩关闭
				btn : [ '确定' ],
				content : '&nbsp;&nbsp;&nbsp;请选择左侧树节点'
			});
		}
	}
	// 删
	$scope.remove = function() {
		if ($scope.index == "") {
			layer.open({
				type : 1,
				title:'删除',
				skin : 'layui-layer-demo', // 样式类名
				closeBtn : 1, // 显示关闭按钮
				shift : 2,
				area : [ '300px', '' ], // 宽高
				shadeClose : false, // 开启遮罩关闭
				btn : [ '确定' ],
				content : '&nbsp;&nbsp;&nbsp;请选择要删除的记录'
			});
		} else {
			var deleteLayer = layer.open({
				type : 1,
				title:'删除',
				skin : 'layui-layer-demo', // 样式类名
				closeBtn : 1, // 显示关闭按钮
				shift : 2,
				area : [ '300px', '' ], // 宽高
				shadeClose : false, // 开启遮罩关闭
				btn : [ '确定', '取消' ],
				yes : function(index, layero) { // 或者使用btn1
					$scope.deleteInfo();
					layer.close(deleteLayer);
				},
				cancel : function(index) { // 或者使用btn2

				},
				content : '&nbsp;&nbsp;&nbsp;您确定要删除吗？'
			});
		}
	}
	// 改
	$scope.modify = function() {
		$scope.actionEvent = 1;
		if ($scope.index == "") {
			layer.open({
				type : 1,
				title:'修改',
				skin : 'layui-layer-demo', // 样式类名
				closeBtn : 1, // 显示关闭按钮
				shift : 2,
				area : [ '300px', '' ], // 宽高
				shadeClose : false, // 开启遮罩关闭
				btn : [ '确定' ],
				content : '&nbsp;&nbsp;&nbsp;请选择要编辑的记录'
			});
		} else {
			$scope.getGrades($scope.schoolId);
			$(".addPage").removeClass("hide");
			$scope.addLayer = layer.open({
				type : 1,
				title:'修改',
				skin : 'layui-layer-demo', // 样式类名
				closeBtn : 1, // 显示关闭按钮
				shift : 2,
				area : [ '400px', '' ], // 宽高
				shadeClose : false, // 开启遮罩关闭
				btn : [ '确定', '取消' ],
				yes : function(index, layero) { // 或者使用btn1
					$scope.saveInfo();
				},
				cancel : function(index) { // 或者使用btn2

				},
				content : $(".addPage")
			});
		}
	}
	// 查
	$scope.find = function() {
		if ($scope.index == "") {
			layer.open({
				type : 1,
				title:'查看',
				skin : 'layui-layer-demo', // 样式类名
				closeBtn : 1, // 显示关闭按钮
				shift : 2,
				area : [ '300px', '' ], // 宽高
				shadeClose : false, // 开启遮罩关闭
				btn : [ '确定' ],
				content : '&nbsp;&nbsp;&nbsp;请选择要查看的记录'
			});
		} else {
			$(".checkPage").removeClass("hide");
			layer.open({
				type : 1,
				title:'查看',
				skin : 'layui-layer-demo', // 样式类名
				closeBtn : 1, // 显示关闭按钮
				shift : 2,
				area : [ '400px', '' ], // 宽高
				shadeClose : false, // 开启遮罩关闭
				btn : [ '关闭' ],
				content : $(".checkPage")
			});
		}
	}
	// checkbox选中
	$scope.changeCheck = function(index, item) {
		if (index == $scope.index) {
			$scope.index = "";
		} else {
			$scope.index = index;
		}
		for (var i = 1; i <= $scope.info.length; i++) {
			if (index != i) {
				$("#check" + i).attr("checked", false);
			}
		}
		$scope.schoolName = item.schoolName;
		$scope.schoolId = item.schoolId;
		$scope.gradeName = item.gradeName;
		$scope.gradeId = item.gradeId;
		$scope.className = item.className;
		$scope.classId = item.classId;
		$scope.studentsNum = item.studentsNum;
		$scope.getGrades(item.schoolId);

	}
	// 年级是否为空
	$scope.$watch('gradeName', function(newValue, oldValue,
			scope) {
		if (newValue != "") {
			$(".add_warning").addClass("hide")
		} else {
			$(".add_warning").removeClass("hide")
		}
	});
	// 班级是否为空
	$scope.$watch('className', function(newValue, oldValue,
			scope) {
		if (newValue != "") {
			$(".add_warning_class").addClass("hide")
		} else {
			$(".add_warning_class").removeClass("hide")
		}
	});
	// 年级未填提示
	$scope.gradeValite = function() {
		layer.tips('该项为必填项', $(".add_warning"), {
			tips : [ 1, '#111' ],
			time : 1000
		});
	}
	// 班级未填提示
	$scope.classValite = function() {
		layer.tips('该项为必填项', $(".add_warning_class"), {
			tips : [ 1, '#111' ],
			time : 1000
		});
	}
});